<template>
  <div class="login-with-qr">
    <!-- 只支持二维码登录 -->
    <QRCodeLogin
      @login-success="handleLoginSuccess"
    />
  </div>
</template>

<script setup lang="ts">
import { useRouter } from 'vue-router'
import { ElMessage } from 'element-plus'
import QRCodeLogin from '@/components/QRCodeLogin.vue'
import { useUserStore } from '@/stores/user'

import { usePermissionStore } from '@/stores/permission';
const router = useRouter()
const userStore = useUserStore()

// 处理登录成功
const handleLoginSuccess = (token: string, userInfo: any) => {
  // 保存用户信息
  userStore.setToken(token)
  userStore.setUserInfo(userInfo)
  
  ElMessage.success('登录成功！')
  const permStore = usePermissionStore();
  permStore.setPermissions(userInfo.permissions);   // 关键一行
  // 跳转到首页或之前的页面
  const redirect = router.currentRoute.value.query.redirect as string
  router.push(redirect || '/')
}
</script>

<style scoped>
.login-with-qr {
  min-height: 100vh;
}
</style>